package com.glela.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.glela.common.constant.CommonConstants;
import com.glela.goods.model.request.ShareGoodsRequest;
import com.glela.goods.model.request.ShareShopRequest;
import com.glela.goods.service.ShareGoodsService;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.util.ParameterUtils;

@Controller
@RequestMapping("shareGoods")
public class ShareGoodsController {

	private static Logger logger = LoggerFactory.getLogger(ShareGoodsController.class);

	@Autowired
	private ShareGoodsService shareGoodsService;

	/**
	 * 查询分享商品,H5用
	 * 
	 * @author 卫耀华
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getShareGoodIdById", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> getShareGoodIdById(@RequestBody ShareGoodsRequest request) {
		try {
			if (ParameterUtils.validParams(request.getShareGoodsId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return shareGoodsService.getShareGoodsByShareIdAndSeeUserId(request.getShareGoodsId(), request.getSeeUserId());
		} catch (Exception e) {
			logger.error("查询分享商品异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}

	/**
	 * 添加商品分享（APP调用）
	 * 
	 * @author 卫耀华
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/addShareGoods", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> addShareGoods(@RequestBody ShareGoodsRequest request) {
		try {
			if (ParameterUtils.validParams(request.getUserId(), request.getGoodId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			
			logger.error("share goods:{}|{}|{}", request.getUserId(),request.getShareGoodsId(),request.getShareSource());
			long materialId = 0l;
			if (!ParameterUtils.validParams(request.getMaterialId())) {
				materialId = request.getMaterialId();
			}
			return shareGoodsService.addShareGoods(request.getUserId(), request.getGoodId(), materialId,request.getLivePlanId());
		} catch (Exception e) {
			logger.error("添加商品分享异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}

	/**
	 * 分享失败要删除之前插入的分享记录（APP调用）
	 * 
	 * @author 卫耀华
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteShareInfoById", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> deleteShareInfoById(@RequestBody ShareGoodsRequest request) {
		try {
			if (ParameterUtils.validParams(request.getShareGoodsId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			shareGoodsService.deleteShareInfoById(request.getShareGoodsId());
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
		} catch (Exception e) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	 * 
	* **********************************************
	* @Title: addStoreShareCount
	* @Description: 添加店铺分享次数
	* @param @param request
	* @param @return    设定文件
	* @return ResponseEntity<?>    返回类型
	* @throws		异常
	* @author NingChongQing		作者
	* @date 2017年7月21日 下午2:53:20		时间
	* **********************************************
	 */
	@RequestMapping(value = "/addStoreShareCount", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> addStoreShareCount(@RequestBody ShareShopRequest request) {
		try {
			if (ParameterUtils.validParams(request.getUserId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			this.shareGoodsService.addStoreShareCount(request.getUserId());
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("添加店铺分享次数异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	* **********************************************
	* @Title: shareOpenStoreGood 
	* @Description: 邀请开店接口
	* @param @param request
	* @param @return 
	* @return ResponseEntity<?>
	* @throws	
	* @author NingChongQing	
	* @date 2018年1月18日 上午9:43:03 
	* **********************************************
	 */
	@RequestMapping(value = "/shareOpenStoreGood", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> shareOpenStoreGood(@RequestBody ShareGoodsRequest request) {
		try {
			if (ParameterUtils.validParams(request.getUserId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return shareGoodsService.shareOpenStoreGood(request.getUserId());
		} catch (Exception e) {
			logger.error("邀请开店异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	* **********************************************
	* @Title: shareActivities 
	* @Description: 活动页分享接口
	* @param @param request
	* @param @return 
	* @return ResponseEntity<?>
	* @throws	
	* @author NingChongQing	
	* @date 2018年1月18日 下午2:03:59 
	* **********************************************
	 */
	@RequestMapping(value = "/shareActivities", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> shareActivities(@RequestBody ShareGoodsRequest shareGoodsRequest, HttpServletRequest request) {
		try {
			if (ParameterUtils.validParams(shareGoodsRequest.getUserId(), shareGoodsRequest.getActiveId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			String clientType = "android";
			if(request.getHeader("phoneType") != null && request.getHeader("phoneType").length() > 0) {
				if(request.getHeader("phoneType").toLowerCase().equals(CommonConstants.PHONE_CLIENT_IOS)){
					clientType = "ios";
				} else if(request.getHeader("phoneType").toLowerCase().equals(CommonConstants.PHONE_CLIENT_ANDROID)) {
					clientType = "android";
				}
			}
			return shareGoodsService.shareActivities(shareGoodsRequest.getUserId(),shareGoodsRequest.getActiveId(),clientType);
		} catch (Exception e) {
			logger.error("活动页分享异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
}
